package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Tiles;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"Aelin", "LM3", "IceCandle", "Taha"}, name="Pinball", version=2.7D)
public class Pinball extends Random
{
  private static final int[] OBJ_PILLARS = { 15000, 15002, 15004, 15006, 15008 };

  private static final int[] OBJ_ACTIVATE = { 15000, 15002, 15004, 15006, 15007, 15008 };
  private static final int INTERFACE_PINBALL = 263;

  public boolean activateCondition()
  {
    return (this.game.isLoggedIn()) && (this.objects.getNearest(OBJ_ACTIVATE) != null);
  }

  private int getScore() {
    RSComponent localRSComponent = this.interfaces.get(263).getComponent(1);
    try {
      return Integer.parseInt(localRSComponent.getText().split(" ")[1]); } catch (ArrayIndexOutOfBoundsException localArrayIndexOutOfBoundsException) {
    }
    return 10;
  }

  public int loop()
  {
    if (!activateCondition()) {
      return -1;
    }

    if ((getMyPlayer().isMoving()) || (getMyPlayer().getAnimation() != -1)) {
      return random(1000, 1600);
    }

    if (getScore() >= 10)
    {
      int i = 15010;
      RSObject localRSObject2 = this.objects.getNearest(new int[] { i });
      if (localRSObject2 != null) {
        if ((this.calc.tileOnScreen(localRSObject2.getLocation())) && (localRSObject2.doAction("Exit"))) {
          sleep(random(2000, 2200));
          localRSObject2.doAction("Exit");
          return random(1000, 1200);
        }
        this.camera.setCompass('s');
        this.walking.walkTileOnScreen(localRSObject2.getLocation());
        return random(1400, 1500);
      }

    }

    RSObject localRSObject1 = this.objects.getNearest(OBJ_PILLARS);

    if (localRSObject1 != null)
    {
      if (this.calc.distanceTo(localRSObject1) > 2) {
        this.walking.walkTileOnScreen(localRSObject1.getLocation());
        return random(500, 600);
      }

      sleep(random(400, 500));

      if (!this.tiles.doAction(localRSObject1.getLocation(), "Tag")) {
        return random(50, 100);
      }
      sleep(500, 1000);

      int j = getScore();
      for (int k = 0; k < 100; k++) {
        if (getScore() > j)
          return random(50, 100);
        sleep(25, 75);
      }
      return random(1000, 1300);
    }
    return random(200, 400);
  }
}